﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Present.Commands.Async.Actions.Decorators;

namespace Present.Commands.Async.Actions
{
    /// <summary>
    /// Listens to an observable and fires canExecutedChanged when the observable returns an item
    /// </summary>
    public class CanExecuteChangedNotifierActionDecorator<TParameter, TResult, TObservable> : AsyncActionDecorator<TParameter, TResult>
    {
        private IDisposable subscription;

        public CanExecuteChangedNotifierActionDecorator(IObservable<TObservable> notifier, IAsyncAction<TParameter, TResult> innerTarget)
            : base(innerTarget)
        {
            subscription = notifier.Subscribe(e => RaiseCanExecuteChanged());
        }
    }

    public static class CanExecuteChangedNotifierAction
    {
        public static IAsyncAction<TParameter, TResult> CanExecuteChangedWhen<TParameter, TResult, TObservable>(this IAsyncAction<TParameter, TResult> innerTarget, IObservable<TObservable> notifier)
        {
            return new CanExecuteChangedNotifierActionDecorator<TParameter, TResult, TObservable>(notifier, innerTarget);
        }
    }
}
